//
//  BLTabBar.m
//  新浪微博
//
//  Created by hbl on 16/2/14.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLTabBar.h"
#import "BLTabBarButton.h"
@interface BLTabBar()
@property (nonatomic, strong)UITabBarItem *item;
@property (nonatomic, strong)BLTabBarButton *currentSelectBtn;
@property (nonatomic, strong)UIButton *centerButton;
@property (nonatomic, strong)NSMutableArray *tabBarButtons;
@end
@implementation BLTabBar

- (NSMutableArray *)tabBarButtons
{
    if (!_tabBarButtons) {
        _tabBarButtons = [NSMutableArray array];
    }
    return _tabBarButtons;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *centerBtn = [[UIButton alloc]init];
        [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_os7"] forState:UIControlStateNormal];
        [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted_os7"] forState:UIControlStateHighlighted];
        [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted_os7"] forState:UIControlStateHighlighted];
        [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_os7"] forState:UIControlStateNormal];
        [centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:centerBtn];
         self.centerButton = centerBtn;
        self.centerButton.bounds = CGRectMake(0, 0,  self.centerButton.currentBackgroundImage.size.width,  self.centerButton.currentBackgroundImage.size.height);

    }
    return self;
}
- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    _item = item;
    BLTabBarButton *btn = [[BLTabBarButton alloc]init];
    btn.item = item;
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
    [self.tabBarButtons addObject:btn];
    [self addSubview:btn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.centerButton.center = CGPointMake(self.frame.size.width * 0.5,self.frame.size.height * 0.5);
    CGFloat btnW = self.frame.size.width/self.subviews.count ;
    CGFloat btnH = self.frame.size.height;
    
    for (int i = 0; i< self.tabBarButtons.count; i++) {
               BLTabBarButton *btn = self.tabBarButtons[i];
        btn.tag = i;
        CGFloat btnX = btnW * i;
        if (i>1) {
            btnX += btnW;
        }
        btn.frame = CGRectMake(btnX, 0, btnW, btnH);
        if (i == 0) {
            btn.selected = YES;
            self.currentSelectBtn = btn;
        }
    }
}

- (void)onClick:(BLTabBarButton *)btn
{
    self.currentSelectBtn.selected = NO;
    btn.selected = YES;
    self.currentSelectBtn = btn;
    if ([self.delegate respondsToSelector:@selector(onTabBarButtonClick:)]) {
        [self.delegate onTabBarButtonClick:btn.tag];
    }
}

- (void)centerBtnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(onCenterButtonClick)]) {
        [self.delegate onCenterButtonClick];
    }
}
@end
